<?php

App::Import('Core', 'ConnectionManager');

class CollectionsController extends AppController {

    var $name = 'Collections';
    var $uses = array();
    var $helpers = array('Ftpman', 'Javascript', 'Session');
    var $components = array('Auth', 'Session');

    function __construct() {
        parent::__construct();
        $this->ftpman = ConnectionManager::getDataSource('ftpman');
    }

    function beforeFilter() {
        parent::beforeFilter();
		$this->Auth->deny('*');
        //$this->Auth->allow('index', 'download_from_ftp', 'download_as_zip');
    } 

    	
    /**
     * 显示用户收藏文件的信息的列表。
     */
    function index() {
		 
		$this->pageTitle  = '我的收藏';
		
		$username = $this->Session->read('Auth.User.username');
		
		$user = $this->ftpman->user($username);
		
		$collections = $user->ls();
		//$this->set('collections_dirs', $collections['dirs']);
        $this->set('collections_info', $collections);
		
	}
	
	/**
     * 删除一个用户的收藏文件。
     */
	function del_collect($path) {
		$path = base64_decode($path);
        $url = '/collections/index/';
		$username = $this->Session->read('Auth.User.username');
        try {
            $entry = $this->ftpman->user($username);
            $entry->del($path);
        } catch (Exception $e) {
            $message = "未能删除 $path: ".$e->getMessage();
            $this->Session->setFlash($message, 'default', array('class' => 'failure'));
            $this->redirect($url);
            return;
        }

        $message = "已删除 $path";
        $this->Session->setFlash($message, 'default', array('class' => 'success'));
        $this->redirect($url);
		
	}
	
	function add_collect($path) {
		$path = base64_decode($path);
        $url = $this->referer();
		$username = $this->Session->read('Auth.User.username');
        try {
            $entry = $this->ftpman->user($username);
            $entry->add($path);
        } catch (Exception $e) {
            $message = "未能添加 $path: ".$e->getMessage();
            $this->Session->setFlash($message, 'default', array('class' => 'failure'));
            $this->redirect($url);
            return;
        }

        $message = "已添加 $path";
        $this->Session->setFlash($message, 'default', array('class' => 'success'));
        $this->redirect($url);
	}
}
?>
